<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="queryParams" ref="queryParamsRef" :inline="true" v-show="showSearch" label-width="100px">
        <el-form-item label="诊断编码类型" prop="diagnosisType">
          <el-select class="width-120" v-model="queryParams.diagnosisType"
            placeholder="请选择诊断编码类型 DIAGNOSIS_CLINIC_DICT字典值" clearable @change="diagnosisTypeChange">
            <el-option v-for="dict in dictDataMap['diagnosis_code_dict']" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue" />
          </el-select>
        </el-form-item>
        <el-form-item label="中西医诊断" prop="chineseWesternType">
          <el-select class="width-120" v-model="queryParams.chineseWesternType" placeholder="请选择中西医诊断区分(0中医1西医2中西医)"
            clearable>
            <el-option v-for="dict in dictDataMap['chinese_western_type']" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue" />
          </el-select>
        </el-form-item>
        <el-form-item label="ICD编码" prop="code">
          <el-input v-model="queryParams.code" placeholder="请输入ICD-10编码" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="名称" prop="zhongwenMingcheng">
          <el-input v-model="queryParams.zhongwenMingcheng" placeholder="请输入中文名称" clearable
            @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item>
          <query-form-button @handleQuery="handleQuery" @resetQuery="resetQuery" />
        </el-form-item>
      </el-form>
    </div>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain @click="handleAdd" v-hasPerm="['base:hisDataIcd10:add']"><template
            #icon><i-ep-plus /></template>新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain @click="handleExport" v-hasPerm="['base:hisDataIcd10:export']"><template #icon>
            <i-ep-download />
          </template>导出
        </el-button>
      </el-col>
    </el-row>

    <el-table v-adaptive :data="hisDataIcd10List" :border="true" :stripe="true" highlight-current-row
      :header-cell-style="$headerCellStyle">
      <el-table-column label="ICD编码" :align="'center'" prop="code" />
      <el-table-column label="诊断编码类型" :align="'center'" prop="diagnosisType">
        <template #default="scope">
        </template>
      </el-table-column>
      <el-table-column label="中文名称" :align="'center'" prop="zhongwenMingcheng" />
      <el-table-column label="英文名称" :align="'center'" prop="yingwenMingcheng" />
      <el-table-column label="具体说明内容" :align="'center'" prop="shuoming" />
      <el-table-column label="其它说明" :align="'center'" prop="otherInfo" />
      <el-table-column label="中西医诊断" :align="'center'" prop="chineseWesternType">
        <template #default="scope">
        </template>
      </el-table-column>
      <el-table-column label="操作" :align="'center'" class-name="small-padding fixed-width" width="220">
        <template #default="scope">
          <el-button type="success" @click="handleUpdate(scope.row)" v-hasPerm="['base:hisDataIcd10:edit']"> <template
              #icon>
              <i-ep-edit />
            </template>修改
          </el-button>
          <el-button type="danger" @click="handleDelete(scope.row)" v-hasPerm="['base:hisDataIcd10:remove']"> <template
              #icon>
              <i-ep-delete />
            </template>删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改数据，ICD编码对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="ICD编码" prop="code">
          <el-input v-model="form.code" placeholder="请输入ICD-10编码" />
        </el-form-item>
        <el-form-item label="诊断编码类型" prop="diagnosisType">
          <el-select v-model="form.diagnosisType" placeholder="请选择诊断编码类型">
            <el-option v-for="dict in dictDataMap['diagnosis_code_dict']" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="中西医诊断区分" prop="chineseWesternType">
          <el-select v-model="form.chineseWesternType" placeholder="请选择中西医诊断区分">
            <el-option v-for="dict in dictDataMap['chinese_western_type']" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="中文名称" prop="zhongwenMingcheng">
          <el-input v-model="form.zhongwenMingcheng" placeholder="请输入中文名称" />
        </el-form-item>
        <el-form-item label="拼音检索码" prop="pinyinIndex">
          <el-input v-model="form.pinyinIndex" placeholder="请输入拼音检索码" />
        </el-form-item>
        <el-form-item label="简短说明" prop="keywordShuoming">
          <el-input v-model="form.keywordShuoming" placeholder="请输入用于检索的关键词，同时也是简短说明" />
        </el-form-item>
        <el-form-item label="英文名称" prop="yingwenMingcheng">
          <el-input v-model="form.yingwenMingcheng" placeholder="请输入英文名称" />
        </el-form-item>
        <el-form-item label="具体说明内容" prop="shuoming">
          <el-input v-model="form.shuoming" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="其它说明" prop="otherInfo">
          <el-input v-model="form.otherInfo" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts">
export default {
  name: "HisDataIcd10"
}
</script>

<script setup lang="ts">
import { addHisDataIcd10, delHisDataIcd10, getHisDataIcd10, selectPageHisDataIcd10, updateHisDataIcd10 } from "@/api/base/hisDataIcd10";
import { useDictStoreHook } from "@/store/modules/dict"
import { useUserStoreHook } from "@/store/modules/user";
const dictStore = useDictStoreHook()
let dictDataMap = ref(dictStore.isTypeGetData('diagnosis_code_dict', 'chinese_western_type'))
const { proxy } = getCurrentInstance() as any;
const useUserStore = useUserStoreHook();
// 登录用户信息
const userInfo = <UserInfoBusi>useUserStore.user.userInfo;
const formRef = ref();
const queryParamsRef = ref()
const hisDataIcd10List = ref([]);
const open = ref(false);
const buttonLoading = ref(false);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const form = ref<any>({
  pid: 0,
  code: "",
  diagnosisType: "",
  zhongwenMingcheng: "",
  pinyinIndex: "",
  keywordShuoming: "",
  yingwenMingcheng: "",
  shuoming: "",
  otherInfo: "",
  subDirectNumber: '',
  subTotalNumber: '',
  chineseWesternType: "",
})
const queryParams = ref<any>({
  pageNum: 1,
  pageSize: 10,
  code: "",
  diagnosisType: "",
  zhongwenMingcheng: "",
  pinyinIndex: "",
  keywordShuoming: "",
  yingwenMingcheng: "",
  chineseWesternType: "",
})
const rules = ref({
  // id: [
  //   {
  //     required: true, message: "主键不能为空", trigger: "blur"
  //   }
  // ],
})
/** 查询数据，ICD编码列表 */
function getList() {
  selectPageHisDataIcd10(queryParams.value).then((response: any) => {
    hisDataIcd10List.value = response.rows;
    total.value = response.total;
  });
}
if (userInfo.orgId) {
  queryParams.value.orgId = userInfo.orgId;
  getList();
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: "",
    pid: 0,
    code: "",
    diagnosisType: "",
    zhongwenMingcheng: "",
    pinyinIndex: "",
    keywordShuoming: "",
    yingwenMingcheng: "",
    shuoming: "",
    otherInfo: "",
    subDirectNumber: 0,
    subTotalNumber: 0,
    chineseWesternType: "",
    delFlag: ""
  };
  nextTick(() => {
    formRef.value.resetFields()
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  queryParamsRef.value.resetFields()
  // proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd() {
  // if (!queryParams.value.orgId) {
  //   return ElMessage.error("请先选择机构")
  // }
  open.value = true;
  reset();
  title.value = "添加数据";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  open.value = true;
  reset();
  const _id = row.id || ids.value
  getHisDataIcd10(_id).then((response: any) => {
    form.value = response.data;
    title.value = "修改数据";
  });
}

/** 提交按钮 */
function submitForm() {
  (formRef.value as any).validate(valid => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        updateHisDataIcd10(form.value).then(() => {
          ElMessage.success("修改成功");
          open.value = false;
          getList();
        }).finally(() => {
          buttonLoading.value = false;
        });
      }
      else {
        addHisDataIcd10(form.value).then(() => {
          ElMessage.success("新增成功");
          open.value = false;
          getList();
        }).finally(() => {
          buttonLoading.value = false;
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id ||
    ids.value;
  ElMessageBox.confirm('是否确认删除ICD编码编号为"' + ids + '"的数据项？',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {
    delHisDataIcd10(_ids).then(() => {
      getList();
      ElMessage.success("删除成功");
    })
  }).catch(() => {
  })
}

function diagnosisTypeChange(value) {
  queryParams.value.diagnosisType = value
  getList()
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/hisDataIcd10/export', {
    ...queryParams.value
  }, `hisDataIcd10_${new Date().getTime()}.xlsx`)
}

// getList();
</script>
